In [50]:
users = {"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0,
"Norah Jones": 4.5, "Phoenix": 5.0,
"Slightly Stoopid": 1.5,
"The Strokes": 2.5, "Vampire Weekend": 2.0},
"Bill": {"Blues Traveler": 2.0, "Broken Bells": 3.5,
"Deadmau5": 4.0, "Phoenix": 2.0,
"Slightly Stoopid": 3.5, "Vampire Weekend": 3.0},
"Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0,
"Deadmau5": 1.0, "Norah Jones": 3.0,
"Phoenix": 5, "Slightly Stoopid": 1.0},
"Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0,
"Deadmau5": 4.5, "Phoenix": 3.0,
"Slightly Stoopid": 4.5, "The Strokes": 4.0,
"Vampire Weekend": 2.0},
"Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0,
"Norah Jones": 4.0, "The Strokes": 4.0,
"Vampire Weekend": 1.0},
"Jordyn": {"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0,
"Phoenix": 5.0, "Slightly Stoopid": 4.5,
"The Strokes": 4.0, "Vampire Weekend": 4.0},
"Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0,
"Norah Jones": 3.0, "Phoenix": 5.0,
"Slightly Stoopid": 4.0, "The Strokes": 5.0},
"Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0,
"Phoenix": 4.0, "Slightly Stoopid": 2.5,
"The Strokes": 3.0}}
Angelica | Bill | Differences | |
---|---|---|---|
Blues Traveler | 3.5 | 2 | 1.5 |
Broken Bells | 2 | 3.5 | 1.5 |
Deadmau | 5 | - | - |
Norah Jones | 4.5 | - | |
Phoenix | 5 | 2 | 3 |
Slightly Stoopid | 1.5 | 3.5 | 2 |
The Strokes | 2.5 | - | - |
Vampire Weekend | 2 | 3 | 1 |
Manhattan Distance: | 9 |
In [51]:
rating1 = users["Angelica"]
In [52]:
rating2 = users["Bill"]
In [53]:
def manhattan(rating1, rating2):
distance = 0
for key in rating1:
if key in rating2:
#print key
distance += abs(rating1[key] - rating2[key])
return distance
In [54]:
manhattan(rating1, rating2)
Out[54]:
In [66]:
def computeNearestNeighbor(username, users):
distances = []
for user in users:
if user != username:
distance = manhattan(users[username], users[user])
distances.append((distance, user))
distances.sort()
return distances
In [67]:
computeNearestNeighbor("Angelica", users)
Out[67]:
In [68]:
computeNearestNeighbor("Hailey", users)
Out[68]:
In [102]:
def recommend(username, nearestFunc):
nearest = nearestFunc(username, users)[0][1]
recommends = []
for artist in users[nearest]:
if not artist in users[username]:
recommends.append((artist, users[nearest][artist]))
return sorted(recommends, key = lambda x: x[1], reverse = True)
#return sorted(recommends, lambda x, y: cmp(x[1], y[1]), reverse = True)
In [103]:
recommend("Hailey", computeNearestNeighbor)
Out[103]:
In [90]:
recommend("Dan")
Out[90]:
In [114]:
def minkowski(rating1, rating2, r):
distance = 0
for artist in rating1:
if artist in rating2:
distance += pow(abs(rating1[artist] - rating2[artist]), r)
return pow(distance, 1/r)
In [115]:
minkowski(rating1, rating2, 1)
Out[115]:
In [116]:
minkowski(rating1, rating2, 2)
Out[116]:
In [120]:
def euclidean(rating1, rating2):
return minkowski(rating1, rating2, 2)
In [121]:
euclidean(rating1, rating2)
Out[121]:
In [ ]: